package com.wcgksoft.controller.goods;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.admin.Account;
import com.wcgksoft.entity.goods.AddService;
import com.wcgksoft.entity.goods.Goods;
import com.wcgksoft.entity.order.Order;
import com.wcgksoft.service.goods.AddServiceService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

/**
 * @author 阿良
 * @version 1.0.0
 * @ClassName AddServiceController.java
 * @Description TODO 增值服务
 * @createTime 2020年05月20日 21:47:00
 */
@Controller
@RequestMapping("addservice/list")
public class AddServiceController {
  @Autowired
  private AddServiceService addServiceService;

  @ResponseBody
  @RequestMapping(value = "find")
  public Layui findAll(
          @RequestParam(value = "page", required = false) Integer page,
          @RequestParam(value = "limit", required = false) Integer limit) {
    DetachedCriteria criteria = DetachedCriteria.forClass(AddService.class);
    List<AddService> list = addServiceService.findByConditions(criteria, page, limit);
    return Layui.data(0, "查询成功", addServiceService.getTotalCount(), list);
  }
  @ResponseBody
  @RequestMapping(value = "update")
  public Layui update(HttpServletRequest request,
                      @RequestParam(value = "data", required = false) String data) throws Exception {
    JSONObject param = JSONObject.fromObject(data);
    if (param == null) {
      return Layui.data(1, "请求格式有误", 0, null);
    } else {
      AddService addService = (AddService) JSONObject.toBean(JSONObject.fromObject(data), AddService.class);
      String id =(String) param.get("id");
      if ("".equals(id)){
        addService.setBeginTime(new Date());
        addServiceService.save(addService);
      }else{
        addServiceService.saveMerge(addService);
      }
      return Layui.data(0, "保存成功", 1, Layui.addList(addService));
    }
  }
  /**
   * 删除
   *
   * @param id
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "delete")
  @Transactional
  public Layui delete(@RequestParam(value = "id") Integer id) {
    System.out.println("id" + id);
    AddService byId = addServiceService.findById(id);
    addServiceService.delete(byId);

    return Layui.data(1, "删除成功", 1, Layui.addList(byId));
  }
}
